<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shopstaff;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;

class RoomService
{


    public function updateRoomFree($redis,$shop_id)
    {

        $room_key = "room_count_".$shop_id;

        $shopRoomModel = new Shoproom();
        $roomValue = $shopRoomModel->where('shop_id',$shop_id)->where('status','free')->count('id');
        $redis->set($room_key,$roomValue);

    }


    /**
     * 保洁，和 新增预定数据
     */
    public function addBookData($order){

        $shopRoomBookModel = new Shoproombook();
        $db = array();
        $db['user_id'] = $order['user_id'];
        $db['room_id'] = $order['room_id'];
        $db['shop_id'] = $order['shop_id'];
        $db['order_id'] = $order['id'];
        $db['start_time'] = $order['book_start'];
        $db['end_time'] = $order['book_end'];
        $db['status'] ='undo';
        $db['type'] ='working';
        $shopRoomBookModel->insert($db);


        $staffModel = new Shopstaff();
        $staff = $staffModel->where('shop_id',$order['shop_id'])->find();
        $user_id = -1;
        if($staff){
            $user_id = $staff['user_id'];
        }
        /**
         * 保洁
         */
        $roomCleanModel = new Roomclean();
        $end = strtotime("+30 minute", $order['book_end']);
        $roomCleanModel->insert(['start_time'=>$order['book_end'],
            'shop_id'=>$order['shop_id'],'room_id'=>$order['room_id'],'end_time'=>$end,'user_id'=>$user_id,'order_id'=>$order['id']]);


        if($order['tuan_id'] != -1){
            $userTuanModel = new Usertuan();
            $tuan = $userTuanModel->where('tuan_id',$order['tuan_id'])->where('status','unuse')->find();
            if($tuan){
                $userTuanModel->where('id',$tuan['id'])->update(['status'=>'used']);
            }
        }

    }

}